package com.qf.ability.search.controller;

import com.qf.ability.search.entity.SearchParams;
import com.qf.ability.search.service.IGoodsSearch;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/search")
@Slf4j
public class GoodsSearchController {

    @Autowired
    private IGoodsSearch goodsSearch;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/queryGoods")
    public R searchGoods(SearchParams searchParams){
        log.info("[searh goods] - 接收到商品搜索的请求 - {}", searchParams);

        Map<String, Object> resultMap = goodsSearch.search(searchParams);
        return Rs.create(resultMap);
    }

    /**
     * 查询热搜词
     * @return
     */
    @RequestMapping("/queryhots")
    public R queryhots(){
        //通过redis获取热搜结果
        Set<String> hotWords = redisTemplate.opsForZSet().reverseRange("hot_words", 0, 9);
        return Rs.create(hotWords);
    }
}
